Casos Acumulados

Mapas

Casos

Casos / 100k hab

Nacional

Lineal

Duplicación

LATAM - Lineal

LATAM - Logarítmica

AA

BB

Regional

Acumulados - Lineal

Acumulados - Lineal V.2

Acumulados - Logaritmico

Casos Nuevos

Mapas

Casos nuevos

Column 2

Diario y media móvil - Lineal

Media Móvil - logarítmica

Duplicación de la media móvil

Column 3

Lineal

Logaritmica

Fallecidos

Column 1

Column 1

Column 1

Fallecidos Nuevos

Diagnósticos

Diagnósticos Nuevos

---
title: "CE4 - Dashboard COVID-19"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    source_code: embed
    social: menu
    theme: cosmo
    self_contained: FALSE 
    fig_mobile: TRUE
---



```{r libraries, message=F, warning=F}
library(flexdashboard)
library(rio)
library(tidyverse)
library(XML)
library(httr)
library(RCurl)
library(sf)
library(lubridate)
library(leaflet)
library(colorspace)
library(DT)
library(zoo)
library(slider)
library(plotly)
library(waffle)
library(extrafont)
library(plyr)
library(extrafont)
library(waffle)
library(RColorBrewer)
library(fontawesome)
options(scipen=999)
```

```{r imports, message=F, warning=F, include = F, echo =F}
nac <- rio::import("https://github.com/jincio/COVID_19_PERU/blob/master/reportes_minsa.xlsx?raw=true")

dep <- rio::import("https://github.com/jincio/COVID_19_PERU/blob/master/reportes_minsa.xlsx?raw=true", sheet = 2)

pop <- read_csv("data/peru_pop_stratum.csv") %>%
  group_by(dep_adm1) %>%
  dplyr::summarise(pop = sum(N)) %>%
  dplyr::mutate(REGION = toupper(dep_adm1))


Paises_LATAM <- c("Argentina","Bolivia","Brazil","Chile","Colombia","Ecuador","Mexico","Peru","Uruguay","Venezuela")
LATAM <- read_csv ("https://covid.ourworldindata.org/data/owid-covid-data.csv") 

shp <- st_read("Limite_departamental", stringsAsFactors = F)%>% 
  st_transform(4326) %>% 
  dplyr::select(Departamento = NOMBDEP)
```

```{r global, message=F, warning=F}
c.date <- max(dep$Fecha)
y.date <- as.Date(c.date) - 1 
today <- ymd(Sys.Date())
f.date <- min(dep$Fecha)

# https://stackoverflow.com/questions/6461209/how-to-round-up-to-the-nearest-10-or-100-or-x
# Rounds Up to a nice number, defined as a number divisible by those in the vector.
# Some other good choices of the "nice" vector above are: 1:10, c(1,5,10), seq(1, 10, 0.1),c(1,2,4,5,6,8,10)

roundUpNice <- function(x, nice=c(1,2,5,6,10)) {
  if(length(x) != 1) stop("'x' must be of length 1")
  10^floor(log10(x)) * nice[[which(x <= 10^floor(log10(x)) * nice)[[1]]]]
}
# roundUpNice(max(nac$pos.new))/5

```

```{r plotly, message=F, warning=F}
icon_svg_path <- "M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"
# icon_svg_path = "M19.404,6.65l-5.998-5.996c-0.292-0.292-0.765-0.292-1.056,0l-2.22,2.22l-8.311,8.313l-0.003,0.001v0.003l-0.161,0.161c-0.114,0.112-0.187,0.258-0.21,0.417l-1.059,7.051c-0.035,0.233,0.044,0.47,0.21,0.639c0.143,0.14,0.333,0.219,0.528,0.219c0.038,0,0.073-0.003,0.111-0.009l7.054-1.055c0.158-0.025,0.306-0.098,0.417-0.211l8.478-8.476l2.22-2.22C19.695,7.414,19.695,6.941,19.404,6.65z M8.341,16.656l-0.989-0.99l7.258-7.258l0.989,0.99L8.341,16.656z M2.332,15.919l0.411-2.748l4.143,4.143l-2.748,0.41L2.332,15.919z M13.554,7.351L6.296,14.61l-0.849-0.848l7.259-7.258l0.423,0.424L13.554,7.351zM10.658,4.457l0.992,0.99l-7.259,7.258L3.4,11.715L10.658,4.457z M16.656,8.342l-1.517-1.517V6.823h-0.003l-0.951-0.951l-2.471-2.471l1.164-1.164l4.942,4.94L16.656,8.342z"


icon_svg_path <-"M12.871,9.337H7.377c-0.304,0-0.549,0.246-0.549,0.549c0,0.303,0.246,0.55,0.549,0.55h5.494c0.305,0,0.551-0.247,0.551-0.55C13.422,9.583,13.176,9.337,12.871,9.337z M15.07,6.04H5.179c-0.304,0-0.549,0.246-0.549,0.55c0,0.303,0.246,0.549,0.549,0.549h9.891c0.303,0,0.549-0.247,0.549-0.549C15.619,6.286,15.373,6.04,15.07,6.04z M17.268,1.645H2.981c-0.911,0-1.648,0.738-1.648,1.648v10.988c0,0.912,0.738,1.648,1.648,1.648h4.938l2.205,2.205l2.206-2.205h4.938c0.91,0,1.648-0.736,1.648-1.648V3.293C18.916,2.382,18.178,1.645,17.268,1.645z M17.816,13.732c0,0.607-0.492,1.1-1.098,1.1h-4.939l-1.655,1.654l-1.656-1.654H3.531c-0.607,0-1.099-0.492-1.099-1.1v-9.89c0-0.607,0.492-1.099,1.099-1.099h13.188c0.605,0,1.098,0.492,1.098,1.099V13.732z"

infobutton <- list(
  name = "Información",
  icon = list(
    path = icon_svg_path,
    transform = "scale(0.84) translate(-1, 0)"
  ),
  click = htmlwidgets::JS(
    "function(gd) { 
      alert('Muestra los casos acumulados en escala logarítmica desde el día del primer reporte a nivel nacional. Las líneas punteadas corresponden a las pendientes de duplicación de casos cada 1 a 4 días; una pendiente más inclinada implica la duplicación de casos en menor tiempo.');
    }"
  )
)

plotly_config <- function(x) {
  x %>% config(locale = "es",
               displaylogo=F,
               modeBarButtonsToRemove = c("select2d","zoom2d","lasso2d",
                                          "drawclosedpath","drawopenpath",
                                          "hoverClosestCartesian","hoverCompareCartesian",
                                          "toggleHover","toggleSpikelines"),
               modeBarButtonsToAdd = list(infobutton),
               responsive = T
  )
}


plotly_layout <- function(x) {
  x %>% layout(paper_bgcolor="black",
               plot_bgcolor="black",
               hoverdistance = 50,
               hovermode = "closest",
               dragmode="pan",
               margin = list(l=65, r=65, b=40, t=50),
               autosize=T
  )
}

# %>%
#   add_segments(x = "2020-03-15", xend = "2020-03-15", 
#                y = 0, yend=roundUpNice(max(nac$pos.new)),
#                text="2020-04-08",name="Estado de Emergencia",
#                hovertemplate = paste('%{text}'),
#                legendgroup = 'group2',
#                width=2, 
#                line = list(color = "#7aa82a", 
#                            width = 3, 
#                            dash = "dot")
#   ) %>%
#   add_segments(x = "2020-03-26", xend = "2020-03-26", 
#                y = 0, yend=roundUpNice(max(nac$pos.new)),
#                text="2020-04-08",name="1ra ampliación",
#                hovertemplate = paste('%{text}'),
#                legendgroup = 'group2',
#                width=2, 
#                line = list(color = "#7aa82a", 
#                            width = 3, 
#                            dash = "dot")
#                ) %>%
#   add_segments(x = "2020-04-23", xend = "2020-04-23", 
#                y = 0, yend=roundUpNice(max(nac$pos.new)),
#                text="2020-04-08",name="3ra ampliación",
#                hovertemplate = paste('%{text}'),
#                legendgroup = 'group2',
#                width=2, 
#                line = list(color = "#7aa82a", 
#                            width = 3, 
#                            dash = "dot")
#                ) 

```

```{r maps, message=F, warning=F}
map_tiles <- function(x) {
  x %>% 
    addTiles(urlTemplate = 'http://a.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png',
             options = providerTileOptions(minZoom = 5, maxZoom = 6))
}

map_bounds <- function(x) {
  x %>% setMaxBounds(lng1 = -90.648918,
                     lat1 = 4.991423,
                     lng2 = -59.605965,
                     lat2 = -23.920121) 
}

map_poly <-  function(x,y,z) {
  x %>%
    addPolygons(fillColor = pal.cases(log(y)),
                weight = 2,
                opacity = 1,
                color = "white",
                dashArray = "",
                fillOpacity = 0.7,
                highlight = highlightOptions(
                  weight = 5,
                  color = "#666",
                  dashArray = "",
                  fillOpacity = 0.7,
                  bringToFront = TRUE),
                label = z,
                labelOptions = labelOptions(
                  style = list("font-weight" = "normal", padding = "3px 8px"),
                  textsize = "15px",
                  direction = "auto")) 
  
}

```

```{r deps, message=F, warning=F}
#### Procesamiento de los datos por región. ####

dep <- 
  dep %>% 
  dplyr::select(dat = Fecha,
                dep = REGION, 
                pos = Positivos_totales, 
                pos.imp = PositivosImputados_totales,
                pas =Fallecidos, 
                smp =Total_muestras
  ) %>% 
  dplyr::mutate(pas = pas %>% if_else(is.na(.), 0, .),
                dat = as.Date(dat)
  ) %>% 
  group_by(dep
  ) %>% 
  dplyr::mutate(pos.new = pos - lag(pos, n = 1),
                pos.imp.new = pos.imp - lag(pos.imp, n = 1),
                pas.new = lag(pas, n = 1),
                smp.new = lag(smp, n = 1),
                ratio.new = signif(pos.new/smp.new), digits = 3,
                days.start =as.numeric(dat-first(dat), unit="days"),
                dummy = days.start+20,
                dup.1 = exp((log(2)/1)*days.start),
                dup.2 = exp((log(2)/2)*days.start),
                dup.3 = exp((log(2)/3)*days.start),
                dup.4 = exp((log(2)/4)*days.start),
                days.end = difftime(today, dat , units = c("days")),
                mav.pos.new = slide_dbl(pos.new, ~mean(.x, na.rm = TRUE), .before = 6),
  ) %>%
  merge(pop %>% 
          select(dep = REGION, pop)
  ) %>% 
  dplyr::mutate(pos.hab = pos/pop*100000,
                smp.hab = smp/pop*100000,
                pos.new.hab = smp/pop*100000,
                mav.pos.new.hab = mav.pos.new/pop*1000000)

dup.dep <- data.frame(dat = as.Date(seq(1,30, 1)+as.Date(c.date))) %>%
  dplyr::mutate(days.start = as.numeric(difftime(dat,f.date), units="days"),
                dummy = days.start+20,
                dup.1 = exp((log(2)/1)*days.start),
                dup.2 = exp((log(2)/2)*days.start),
                dup.3 = exp((log(2)/3)*days.start),
                dup.4 = exp((log(2)/4)*days.start),
                dep= "LIMA",
                days.end = difftime(today, dat , units = c("days"))
  ) %>%
  bind_rows(dep)
#### Poligonos y data regional - Mapas #### 

geom.dep <- dep %>% 
  merge(shp, by.y = 'Departamento', by.x = 'dep', all.x = T) %>%
  st_as_sf(sf_column_name = 'geometry') 

rm(shp) # No more use for shp

#### Datos a nivel Nacional #### 

nac <- dep %>%
  select(-c("dep")) %>%
  group_by(dat) %>%
  dplyr::mutate_at(vars(-c("dat","dup.1","dup.2","dup.3","dup.4")),sum, na.rm = T) %>%
  dplyr::summarize_all(list(max)) %>%
  dplyr::mutate(days.end = difftime(today, dat , units = c("days")),
                days.start = as.numeric(difftime(dat,f.date), units="days"))

dup.nac <- data.frame(dat = as.Date(seq(1,30, 1)+today)) %>%
  dplyr::mutate(days.start = as.numeric(difftime(dat,f.date), units="days"),
                dummy = days.start+20,
                dup.1 = exp((log(2)/1)*days.start),
                dup.2 = exp((log(2)/2)*days.start),
                dup.3 = exp((log(2)/3)*days.start),
                dup.4 = exp((log(2)/4)*days.start)
  ) %>%
  bind_rows(nac)

#### Datos del día de hoy #### 
c.dep <- geom.dep %>%
  dplyr::filter(dat == c.date)

####  Formato Regiones Wide #### 
dep.pos <-  dep %>%
  select(dat,dep,pos,days.end) %>%
  spread(dep, pos) %>% 
  dplyr::rename(LA_LIBERTAD = `LA LIBERTAD`,
                MADRE_DE_DIOS = `MADRE DE DIOS`,
                SAN_MARTIN = `SAN MARTIN`) 

dep.pos2 <-  dep %>%
  select(dat,dep,pos,days.end,days.start) %>%
  spread(dep, pos) %>% 
  dplyr::rename(LA_LIBERTAD = `LA LIBERTAD`,
                MADRE_DE_DIOS = `MADRE DE DIOS`,
                SAN_MARTIN = `SAN MARTIN`) 

dep.mav.pos.new<-  dep %>%
  dplyr::select(dat,dep,days.end,mav.pos.new) %>%
  spread(dep, mav.pos.new) %>% 
  dplyr::rename(LA_LIBERTAD = `LA LIBERTAD`,
                MADRE_DE_DIOS = `MADRE DE DIOS`,
                SAN_MARTIN = `SAN MARTIN`) 


dep.pos.imp.new <- dep %>%
  select(dat,dep,days.end,pos.imp.new) %>%
  spread(dep, pos.imp.new) %>% 
  dplyr::rename(LA_LIBERTAD = `LA LIBERTAD`,
                MADRE_DE_DIOS = `MADRE DE DIOS`,
                SAN_MARTIN = `SAN MARTIN`) 
######################################
y<- dep.pos.imp.new 
colnames(y) <- paste(colnames(y), "2", sep = "_")

dep.pos_dep.pos.imp.new<- y %>% 
  select(-c("dat_2","days.end_2")) %>% 
  cbind(dep.pos)
######################################

### Wide dup 1 inicial
dep.pos2[nrow(dep.pos2)+30,] <- NA
dup.dep.1 <- dup.dep %>%
  select(dat,dep,days.end,days.start,dup.1) %>%
  spread(dep, dup.1) %>% 
  dplyr::rename(LA_LIBERTAD = `LA LIBERTAD`,
                MADRE_DE_DIOS = `MADRE DE DIOS`,
                SAN_MARTIN = `SAN MARTIN`) 

y<- dup.dep.1
colnames(y) <- paste(colnames(y), "2", sep = "_")

dup.dep.wide <- y %>% 
  select(-c("dat_2","days.end_2","days.start_2")) %>% 
  cbind(dep.pos2)

rm(dep.pos2)
### Wide dup 2
dup.dep.2 <- dup.dep %>%
  select(dat,dep,days.end,days.start,dup.2) %>%
  spread(dep, dup.2) %>% 
  dplyr::rename(LA_LIBERTAD = `LA LIBERTAD`,
                MADRE_DE_DIOS = `MADRE DE DIOS`,
                SAN_MARTIN = `SAN MARTIN`) 

y<- dup.dep.2
colnames(y) <- paste(colnames(y), "3", sep = "_")

dup.dep.wide <- y %>% 
  select(-c("dat_3","days.end_3","days.start_3")) %>% 
  cbind(dup.dep.wide)

### Wide dup 3
dup.dep.3 <- dup.dep %>%
  select(dat,dep,days.end,days.start,dup.3) %>%
  spread(dep, dup.3) %>% 
  dplyr::rename(LA_LIBERTAD = `LA LIBERTAD`,
                MADRE_DE_DIOS = `MADRE DE DIOS`,
                SAN_MARTIN = `SAN MARTIN`) 

y<- dup.dep.3
colnames(y) <- paste(colnames(y), "4", sep = "_")

dup.dep.wide <- y %>% 
  select(-c("dat_4","days.end_4","days.start_4")) %>% 
  cbind(dup.dep.wide)
### Wide dup 4
dup.dep.4 <- dup.dep %>%
  select(dat,dep,days.end,days.start,dup.4) %>%
  spread(dep, dup.4) %>% 
  dplyr::rename(LA_LIBERTAD = `LA LIBERTAD`,
                MADRE_DE_DIOS = `MADRE DE DIOS`,
                SAN_MARTIN = `SAN MARTIN`) 

y<- dup.dep.4
colnames(y) <- paste(colnames(y), "5", sep = "_")

dup.dep.wide <- y %>% 
  select(-c("dat_5","days.end_5","days.start_5")) %>% 
  cbind(dup.dep.wide)

#### LATAM #### 
LATAM <- LATAM %>%
  dplyr::filter(location %in% Paises_LATAM) %>%
  dplyr::mutate()%>% 
  group_by(location)%>% 
  dplyr::mutate(mav_new = slide_dbl(new_cases, ~mean(.x, na.rm = TRUE), .before = 6),
                days.end = difftime(today, date , units = c("days")),
                days.start = as.numeric(difftime(date,f.date), units="days"),
                dummy = days.start+20,
                dup.1 = exp((log(2)/1)*days.start),
                dup.2 = exp((log(2)/2)*days.start),
                dup.3 = exp((log(2)/3)*days.start),
                dup.4 = exp((log(2)/4)*days.start),
                mav.pos.new = slide_dbl(new_cases, ~mean(.x, na.rm = TRUE), .before = 6),
  )


dup.LATAM <- data.frame(date = as.Date(seq(1,30, 1)+as.Date(c.date))) %>%
  dplyr::mutate(days.start = as.numeric(difftime(date,f.date), units="days"),
                dummy = days.start+20,
                dup.1 = exp((log(2)/1)*days.start),
                dup.2 = exp((log(2)/2)*days.start),
                dup.3 = exp((log(2)/3)*days.start),
                dup.4 = exp((log(2)/4)*days.start),
                location= "Peru"
  ) %>%
  bind_rows(LATAM)



```

```{r, message=F, warning=F}
vars.pmav.new <- dep %>%
  dplyr::select(dat,dep,mav.pos.new.hab) %>% 
  dplyr::filter(dat == c.date) %>% 
  dplyr::arrange(dplyr::desc(mav.pos.new.hab)) %>%
  dplyr::select(dep) %>%
  dplyr::mutate(dep = ifelse(dep=="LA LIBERTAD","LA_LIBERTAD",
                             ifelse(dep=="MADRE DE DIOS","MADRE_DE_DIOS",
                                    ifelse(dep=="SAN MARTIN","SAN_MARTIN",dep))))%>% 
  .$dep

vars.mav.new <- dep %>%
  dplyr::select(dat,dep,mav.pos.new) %>% 
  dplyr::filter(dat == c.date) %>% 
  dplyr::arrange(dplyr::desc(mav.pos.new)) %>%
  dplyr::select(dep) %>%
  dplyr::mutate(dep = ifelse(dep=="LA LIBERTAD","LA_LIBERTAD",
                             ifelse(dep=="MADRE DE DIOS","MADRE_DE_DIOS",
                                    ifelse(dep=="SAN MARTIN","SAN_MARTIN",dep))))%>% 
  .$dep
last.mav.new <- vars.mav.new[length(vars.mav.new)]


vars.pos <- dep %>%
  dplyr::select(dat,dep,pos) %>% 
  dplyr::filter(dat == c.date) %>% 
  dplyr::arrange(dplyr::desc(pos)) %>%
  dplyr::select(dep) %>%
  dplyr::mutate(dep = ifelse(dep=="LA LIBERTAD","LA_LIBERTAD",
                             ifelse(dep=="MADRE DE DIOS","MADRE_DE_DIOS",
                                    ifelse(dep=="SAN MARTIN","SAN_MARTIN",dep))))%>% 
  .$dep
last.pos <- vars.pos[length(vars.pos)]

```

Casos Acumulados {.bg}
=====================================  

Mapas {.tabset data-width=200} 
-------------------------------------

### Casos

```{r, message=F, warning=F}
palette_1 <- c("#ffffff",
               "#fff3e3",
               "#ffe8c8",
               "#ffddac",
               "#ffd291",
               "#ffc775",
               "#ffbc59",
               "#ffb139", #ffa600
               "#ec9832",
               "#d8802c",
               "#c26926",
               "#ac5320",
               "#953e1b",
               "#7d2914",
               "#65150d",
               "#4e0000")

labels.total <-  sprintf(
  "%s
Casos: %s", c.dep$dep, c.dep$pos) %>% lapply(htmltools::HTML) pal.cases <- colorNumeric( palette=palette_1, domain = log(c.dep$pos), na.color="transparent") leaflet(c.dep) %>% map_tiles() %>% map_poly (c.dep$pos,labels.total) %>% addLegend("bottomleft", pal = pal.cases, values = log(c.dep$pos), title= 'Casos', labFormat = labelFormat(transform = function(x) round(exp(x)))) %>% map_bounds () ``` ### Casos / 100k hab ```{r, message=F, warning=F} labels.pos.hab <- sprintf( "%s
Casos/100k hab: %s", c.dep$dep, round(c.dep$pos.hab)) %>% lapply(htmltools::HTML) pal.cases <- colorNumeric( palette=palette_1, domain = log(c.dep$pos.hab), na.color="transparent") leaflet(c.dep) %>% map_tiles () %>% map_poly (c.dep$pos.hab,labels.pos.hab) %>% addLegend("bottomleft", pal = pal.cases, values = log(c.dep$pos.hab), title= 'Casos', labFormat = labelFormat(transform = function(x) round(exp(x)))) %>% map_bounds ``` Nacional {.tabset data-width=300} ------------------------------------- ### Lineal ```{r, message=F, warning=F} CA_2_1<- nac %>% plot_ly() %>% add_trace(x = ~dat, y = ~pos.new, type = 'bar', name = 'Casos Nuevos', marker = list(color = '#006b7d'), text = paste(nac$days.end, "días desde hoy"), hovertemplate = paste('Fecha: %{x}', '
Nuevos Casos: %{y}
', '%{text}'), yaxis="y") %>% add_trace(x = ~dat, y = ~pos, type = 'scatter', mode = 'lines+markers', name = 'Casos Acumulados', line = list(color = '#ffa600'), marker = list(color = '#ffa600', maxdisplayed = 0), text = paste(nac$days.end, "días desde hoy"), hovertemplate = ~paste('Fecha: %{x}', "
Casos Acumulados: %{y:.0f}
", '%{text}'), yaxis="y2") %>% add_segments(x = "2020-04-08", xend = "2020-04-08", y = 0, yend=roundUpNice(max(nac$pos.new)), text="2020-04-08",name="Pruebas Rápidas", hovertemplate = paste('%{text}'), width=2, line = list(color = "#008f6a", width = 3, dash = "dot"))%>% layout(title = '', titlefont=list(color="white"), xaxis = list(title = "Fecha de Reporte", color = "white", tickformat= "%d-%b", range = c(as.Date("2020-03-06"), as.Date(c.date))), yaxis = list(side = 'left', title = 'Casos Nuevos por día', showgrid = T, gridcolor = "#818181", zeroline = F, color = "#98cbe1", range=list(0, roundUpNice(max(nac$pos.new))), autotick=F, tick0=0, dtick=roundUpNice(max(nac$pos.new))/5), yaxis2 = list(side = 'right', overlaying = "y3", title = 'Casos acumulados (lineal)', showgrid = F, zeroline = F, color = "#ffd29f", range=list(0, roundUpNice(max(nac$pos))), autotick=F, tick0=0, dtick=roundUpNice(max(nac$pos))/5), annotations = list(text = "Casos nuevos y acumulados - Perú", x = 0.5, y = 1.1, yref = "paper",xref = "paper", xanchor = "middle",yanchor = "middle", showarrow = FALSE, font = list(size = 20, color = "white")), legend = list(orientation = "h", xanchor = "bottom", yanchor = "middle", x = 0.5, y = -0.125, font = list(color = "white")) ) %>% plotly_config() %>% plotly_layout() %>% partial_bundle() ``` ### Duplicación ```{r, message=F, warning=F} CA_2_2<-plot_ly(dup.nac)%>% add_trace(x = ~dat, y = ~dup.1, type = 'scatter', mode = 'lines', name = 'Un (1) día', line = list(color = '#0e5871', dash = "dash", width=3.5), text = "Casos se duplican en un (1) día", hoverinfo = "text") %>% add_trace(x = ~dat, y = ~dup.2, type = 'scatter', mode = 'lines', name = 'Dos (2) días', line = list(color = '#006b7d', dash = "dash", width=3.5), text = "Casos se duplican en dos (2) días", hoverinfo = "text") %>% add_trace(x = ~dat, y = ~dup.3, type = 'scatter', mode = 'lines', name = 'Tres (3) días', line = list(color = '#007e7b', dash = "dash", width=3.5), text = "Casos se duplican en tres (3) días", hoverinfo = "text") %>% add_trace(x = ~dat, y = ~dup.4, type = 'scatter', mode = 'lines', name = 'Cuatro (4) días', line = list(color = '#008f6a', dash = "dash", width=3.5), text = "Casos se duplican en cuatro (4) días", hoverinfo = "text")%>% add_trace(x = ~dat, y = ~pos, type = 'scatter', mode = 'lines+markers', name = 'Casos Acumulados', line = list(color = '#ffa600'), marker = list(color = '#ffa600'), hovertemplate = ~paste("
Casos Acumulados: %{y:.d0}
")) %>% layout(title = list(text= '', font = list( size = 20, color="white")), xaxis = list(title = list(text="Días desde el primer reporte", standoff = 15), range = c(as.Date(min(f.date)),max(today+15)), color ="white", tickformat= "%d-%b", showgrid = F, zeroline = F), yaxis = list(side = 'left', title = list(text= 'Total de casos acumulados', font = list(size = 16, color = "White"), standoff = 15), type="log", automargin = T, range = c(min(0),max(6)), showgrid = T, gridcolor = "#818181", zeroline = FALSE, tickmode = "linear", tick0 = 0, color ="ffd29f"), # legend = list(title=list(text="Casos acumulados se duplican en...", # font = list(color="white"), # side="top"), # orientation = "h", # yref = "paper", # xref = "paper", # xanchor = "right", # yanchor = "bottom", # x = 1, # y = 0.1, # font = list(color = "white", # size = 10), # bgcolor= 'rgba(0,0,0,0.7)', # automargin = T), annotations = list(yref = "paper",xref = "paper", xanchor = "middle",yanchor = "middle", showarrow = FALSE, font = list(size = 20, color = "white"), x=0.5, y=1.1, text='Casos acumulados - Perú', showarrow=FALSE, font = list(size = 20, color = "white")) ) %>% plotly_config( ) %>% plotly_layout () %>% partial_bundle() %>% htmlwidgets::onRender('function(el, x) { $("[data-title=\'Información\'] svg path").css("fill", "#f6e486"); $("[data-title=\'Información\'] svg").css("width","2em"); }')%>% config(displayModeBar = TRUE) CA_2_2 #fa("info-circle") ``` ### LATAM - Lineal ```{r} CA_1_1 <- LATAM %>% dplyr::filter(date>=as.Date("2020-03-06")) %>% highlight_key(~location) %>% plot_ly()%>% add_lines( x = ~date, text = ~location, colors = "YlOrRd", split=~location, y = ~total_cases, mode="lines", yaxis="y2", showlegend=F, line = list(width=0.75) ) %>% filter(location=="Peru") %>% add_trace( x = ~date, y = ~total_cases, type = 'scatter', mode = 'lines', name = 'Casos Acumulados', line = list(color = '#ffa600', width=5), hovertemplate = ~paste('Fecha: %{x}', "
Casos Acumulados: %{y:.0f}
"), yaxis="y2", showlegend=F) %>% highlight(on = "plotly_hover", off = "plotly_doubleclick") %>% layout(title = list(text= '', font = list( size = 20, color="white")), xaxis = list(range = c(as.Date("2020-03-06"), c.date), color = "white", title ="Fecha de Reporte", tickformat= "%d-%b"), yaxis2 = list(side = 'right', title = '', showgrid = T, gridcolor = "#818181", zeroline = F, color = "#ffd29f", range=list(0, roundUpNice(max(LATAM$total_cases))), autotick=F, tick0=0, dtick=roundUpNice(max(LATAM$total_cases))/5), annotations = list(text = "Casos acumulados - América Latina", x = 0.5, y = 1.05, yref = "paper",xref = "paper", xanchor = "middle",yanchor = "middle", showarrow = FALSE, font = list(size = 20, color = "white")), paper_bgcolor="black", plot_bgcolor="black", # legend = list(orientation = "h", # xanchor = "center", # yanchor = "bottom", # x = 0.5, # y = -0.125, # font = list(color = "white")), dragmode="pan", margin = list(l=75, r=0, b=65, t=65), showlegend=F) %>% config(locale = "es", displaylogo=F, modeBarButtonsToRemove = c("select2d","zoom2d","lasso2d", "drawclosedpath","drawopenpath", "hoverClosestCartesian","hoverCompareCartesian", "toggleHover","toggleSpikelines") )%>% partial_bundle() ``` ### LATAM - Logarítmica ```{r} CA_1_2 <- dup.LATAM %>% dplyr::filter(date>=as.Date("2020-03-06")) %>% plot_ly() %>% add_lines( x = ~date, text = ~location, colors = "YlOrRd", split=~location, y = ~total_cases, mode="lines", yaxis="y", showlegend=F, line = list(width=0.75) ) %>% filter(location=="Peru") %>% add_trace( x = ~date, y = ~total_cases, type = 'scatter', mode = 'lines', name = 'Casos Acumulados', line = list(color = '#ffa600', width=5), hovertemplate = ~paste('Fecha: %{x}', "
Casos Acumulados: %{y:.0f}
"), yaxis="y", showlegend=F) %>% add_trace(x = ~date, y = ~dup.1, type = 'scatter', mode = 'lines', name = 'Un (1) día', line = list(color = '#0e5871', dash = "dash", width=3.5), text = "Casos se duplican en un (1) día", hoverinfo = "text", showlegend=F) %>% add_trace(x = ~date, y = ~dup.2, type = 'scatter', mode = 'lines', name = 'Dos (2) días', line = list(color = '#006b7d', dash = "dash", width=3.5), text = "Casos se duplican en dos (2) días", hoverinfo = "text", showlegend=F) %>% add_trace(x = ~date, y = ~dup.3, type = 'scatter', mode = 'lines', name = 'Tres (3) días', line = list(color = '#007e7b', dash = "dash", width=3.5), text = "Casos se duplican en tres (3) días", hoverinfo = "text", showlegend=F) %>% add_trace(x = ~date, y = ~dup.4, type = 'scatter', mode = 'lines', name = 'Cuatro (4) días', line = list(color = '#008f6a', dash = "dash", width=3.5), text = "Casos se duplican en cuatro (4) días", hoverinfo = "text", showlegend=F) %>% layout(title = list(text= '', font = list( size = 20, color="white")), xaxis = list(range = c(as.Date("2020-03-06"), max(dup.LATAM$date,na.rm=T)), color = "white", title ="Fecha de Reporte", tickformat= "%d-%b", showgrid=F), yaxis = list(side = 'left', title = list(text= 'Total de casos acumulados', font = list(size = 16, color = "ffd29f"), standoff = 15), type="log", automargin = T, range = c(min(0),max(6)), showgrid = F, zeroline = FALSE, tickmode = "linear", tick0 = 0, color ="White"), annotations = list(text = "Casos acumulados - América Latina", x = 0.5, y = 1.05, yref = "paper",xref = "paper", xanchor = "middle",yanchor = "middle", showarrow = FALSE, font = list(size = 20, color = "white")), paper_bgcolor="black", plot_bgcolor="black", # legend = list(orientation = "h", # xanchor = "center", # yanchor = "bottom", # x = 0.5, # y = -0.125, # font = list(color = "white")), dragmode="pan", margin = list(l=75, r=0, b=65, t=65), showlegend=F) %>% config(locale = "es", displaylogo=F, modeBarButtonsToRemove = c("select2d","zoom2d","lasso2d", "drawclosedpath","drawopenpath", "hoverClosestCartesian","hoverCompareCartesian", "toggleHover","toggleSpikelines") )%>% partial_bundle() ``` ### AA ```{r} fig <- subplot(CA_1_1,CA_2_1, nrows = 2, shareX=T,titleX=F,margin=0.04) %>% partial_bundle() fig <- fig %>% layout(xaxis2 = list(domain=list(x=c(0,1),y=c(0,0.5))), xaxis = list(domain=list(x=c(0,1),y=c(0.5,1))), legend = list(orientation = "h", xanchor = "center", yanchor = "bottom", x = 0.5, y = -0.125, font = list(color = "white")), annotations = list( list(text = "Fecha de reporte", x = 0.5, y = -0.09, yref = "paper", xref = "paper", showarrow = FALSE, font = list(size = 16, color = "white")), list(text = "Casos nuevos por día", x = -0.1225, y = 0.1, yref = "paper", xref = "paper", showarrow = FALSE, font = list(size = 16, color ="#98cbe1"), textangle = -90), list(text = "Casos acumulados (lineal)", x = 1.1225, y = 0.8, yref = "paper", xref = "paper", showarrow = FALSE, font = list(size = 16, color ="#ffd29f"), textangle = -90)), margin = list(l=60, r=60, b=20, t=50))%>% partial_bundle() fig ``` ### BB ```{r} fig <- subplot(CA_1_2,CA_2_2, nrows = 2, shareX=T,titleX=F,margin=0.04) %>% partial_bundle() fig <- fig %>% layout(xaxis2 = list(domain=list(x=c(0,1),y=c(0,0.5))), xaxis = list(domain=list(x=c(0,1),y=c(0.5,1))), legend = list(orientation = "h", font = list(color = "white", size=10), x = 0.5, y = -0.09, yref = "paper", xref = "paper", xanchor="center", yanchor="middle"), annotations = list( list(text = "Fecha de reporte", x = 0.5, y = -0.09, yref = "paper", xref = "paper", showarrow = FALSE, font = list(size = 16, color = "white")), list(text = "Casos acumulados (Logarítmico)", x = -0.1225, y = 0.1, yref = "paper", xref = "paper", showarrow = FALSE, font = list(size = 16, color ="White"), textangle = -90) # list(text = "Casos acumulados (lineal)", # x = 1.1225, # y = 0.8, # yref = "paper", # xref = "paper", # showarrow = FALSE, # font = list(size = 16, # color ="#ffd29f"), # textangle = -90)), ), margin = list(l=60, r=60, b=20, t=50))%>% partial_bundle() fig ``` Regional {.tabset data-width=500} ------------------------------------- ### Acumulados - Lineal ```{r, message=F, warning=F} plots <- lapply(vars.pos, function(var) { plot_ly(dep.pos_dep.pos.imp.new)%>% add_trace(x = ~dat, y = as.formula(paste0("~", var,"_2")), type = 'bar', name = 'Casos Nuevos', marker = list(color = '#006b7d'), text = paste(dep.pos.imp.new$days.end, "días desde hoy"), hovertemplate = paste('Fecha: %{x}', '
Nuevos Casos: %{y}
', '%{text}'), showlegend = ifelse(var == last.pos,TRUE,FALSE), yaxis="y" ) %>% add_lines(x = ~dat, y = as.formula(paste0("~", var)), text = paste(dep.pos$days.end, "días desde hoy"), hovertemplate = paste('Fecha: %{x}', '
Media Móvil: %{y:.2f}
', '%{text}'), name = ifelse(var == last.pos,"Media Móvil",var), legendgroup = 'group1', showlegend = ifelse(var == last.pos,TRUE,FALSE), line = list(color = "#ffa600", width = 4), yaxis="y2" ) %>% add_segments(x = "2020-04-08", xend = "2020-04-08", y = 0, yend = max(dep.pos[var],na.rm = T), text="2020-04-08", name="Inicio de P.R's", hovertemplate = paste('%{text}'), legendgroup = 'group2', showlegend = ifelse(var == last.pos,TRUE,FALSE), line = list(color = "#7aa82a", width = 3, dash = "dot") ) %>% layout(xaxis = list(range = c(min(dep.pos.imp.new$dat), max(dep.pos.imp.new$dat)), color = "white", tickformat= "%d-%b"), yaxis = list(side = 'left', title = 'Casos Nuevos por día', showgrid = T, gridcolor = "#818181", zeroline = F, color = "#98cbe1", range=list(0, roundUpNice(max(dep.pos_dep.pos.imp.new[paste0(var,"_2")],na.rm = T))), autotick=F, tick0=0, dtick=roundUpNice(max(dep.pos_dep.pos.imp.new[paste0(var,"_2")],na.rm = T))/5), yaxis2 = list(side = 'right', overlaying = paste0("y",ifelse(which(vars.pos==var)==1, "", 2*which(vars.pos==var)-1)), title = '', showgrid = F, zeroline = F, color = "#ffd29f", range=list(0, roundUpNice(max(dep.pos_dep.pos.imp.new[var],na.rm = T))), autotick=F, tick0=0, dtick=roundUpNice(max(dep.pos_dep.pos.imp.new[var],na.rm = T))/5), annotations = list(text = ifelse(var=="LA_LIBERTAD","LA LIBERTAD", ifelse(var=="MADRE_DE_DIOS","MADRE DE DIOS", ifelse(var=="SAN_MARTIN","SAN MARTIN",paste0("",var,"")))), x = 0,y = 1.2, yref = "paper",xref = "paper", xanchor = "left",yanchor = "top", showarrow = FALSE, font = list(size = 14, color = "white")), paper_bgcolor="black", plot_bgcolor="black", legend = list(orientation = "h", xanchor = "center", yanchor = "bottom", x = 0.5, y = -0.125, font = list(color = "white")), showlegend =T)%>% partial_bundle() }) subplot(plots,nrows=5, shareX = T,titleX=F, margin = 0.0375) %>% layout(title = list(text = "Total de casos confirmados - Acumulado Lineal", font = list(size = 24, color="white")), annotations = list( list(text = "Fecha de reporte", x = 0.5, y = -0.09, yref = "paper", xref = "paper", showarrow = FALSE, font = list(size = 16, color = "white")), list(text = "Casos nuevos por día", x = -0.05, y = 0.7, yref = "paper", xref = "paper", showarrow = FALSE, font = list(size = 16, color ="#98cbe1"), textangle = -90), list(text = "Casos acumulados (lineal)", x = 1.05, y = 0.7, yref = "paper", xref = "paper", showarrow = FALSE, font = list(size = 16, color ="#ffd29f"), textangle = -90)), hovermode = "closest", hoverdistance = 10, dragmode="pan", margin = list(l=50, r=50, b=75, t=60) ) %>% config(locale = "es", displaylogo=F, modeBarButtonsToRemove = c("select2d","zoom2d","lasso2d", "drawclosedpath","drawopenpath", "hoverClosestCartesian","hoverCompareCartesian", "toggleHover","toggleSpikelines") )%>% partial_bundle() ``` ### Acumulados - Lineal V.2 ```{r, message=F, warning=F} plots <- lapply(vars.pos, function(var) { dup.dep.wide %>% select(days.start, paste0(var), paste0(var,"_2"), paste0(var,"_3"), paste0(var,"_4"), paste0(var,"_5")) %>% group_by(days.start) %>% summarize_all(list(max),na.rm=T) %>% plot_ly() %>% add_lines(x = ~days.start, y = as.formula(paste0("~", var)), hovertemplate = paste('Fecha: %{x}', '
Media Móvil: %{y:.2f}
'), name = ifelse(var == last.pos,"Media Móvil",var), legendgroup = 'group1', showlegend = ifelse(var == last.pos,TRUE,FALSE), line = list(color = "#ffa600", width = 4), yaxis="y" ) %>% add_segments(x = "2020-04-08", xend = "2020-04-08", y = 0, yend = max(dep.pos[var],na.rm = T), text="2020-04-08", name="Inicio de P.R's", hovertemplate = paste('%{text}'), legendgroup = 'group2', showlegend = ifelse(var == last.pos,TRUE,FALSE), line = list(color = "#7aa82a", width = 3, dash = "dot") ) %>% add_trace(x = ~days.start, y = as.formula(paste0("~", var,"_2")), type = 'scatter', mode = 'lines', name = 'Un (1) día', line = list(color = '#0e5871', dash = "dash", width=3.5), text = "Casos se duplican en un (1) día", hoverinfo = "text", showlegend=F) %>% add_trace(x = ~days.start, y = as.formula(paste0("~", var,"_3")), type = 'scatter', mode = 'lines', name = 'Dos (2) días', line = list(color = '#006b7d', dash = "dash", width=3.5), text = "Casos se duplican en dos (2) días", hoverinfo = "text", showlegend=F) %>% add_trace(x = ~days.start, y = as.formula(paste0("~", var,"_4")), type = 'scatter', mode = 'lines', name = 'Tres (3) días', line = list(color = '#007e7b', dash = "dash", width=3.5), text = "Casos se duplican en tres (3) días", hoverinfo = "text", showlegend=F) %>% add_trace(x = ~days.start, y = as.formula(paste0("~", var,"_5")), type = 'scatter', mode = 'lines', name = 'Cuatro (4) días', line = list(color = '#008f6a', dash = "dash", width=3.5), text = "Casos se duplican en cuatro (4) días", hoverinfo = "text", showlegend=F) %>% layout(xaxis = list(range = c(min(dup.dep.wide$days.start), max(dup.dep.wide$days.start)), color = "white"), yaxis = list(side = 'left', title = list(text= 'Total de casos acumulados', font = list(size = 16, color = "White"), standoff = 15), type="log", automargin = T, range = c(min(0),max(6)), showgrid = T, gridcolor = "#818181", zeroline = FALSE, tickmode = "linear", tick0 = 0, color ="White"), annotations = list(text = ifelse(var=="LA_LIBERTAD","LA LIBERTAD", ifelse(var=="MADRE_DE_DIOS","MADRE DE DIOS", ifelse(var=="SAN_MARTIN","SAN MARTIN",paste0("",var,"")))), x = 0,y = 1.2, yref = "paper",xref = "paper", xanchor = "left",yanchor = "top", showarrow = FALSE, font = list(size = 14, color = "white")), paper_bgcolor="black", plot_bgcolor="black", legend = list(orientation = "h", xanchor = "center", yanchor = "bottom", x = 0.5, y = -0.125, font = list(color = "white")), showlegend =T)%>% partial_bundle() }) subplot(plots,nrows=5, shareX = T,titleX=F, margin = 0.0375) %>% layout(title = list(text = "Total de casos confirmados - Regiones", font = list(size = 24, color="white")), annotations = list( list(text = "Días desde el primer reporte", x = 0.5, y = -0.09, yref = "paper", xref = "paper", showarrow = FALSE, font = list(size = 16, color = "white")), list(text = "Casos acumulados (Logarítmica)", x = -0.05, y = 0.7, yref = "paper", xref = "paper", showarrow = FALSE, font = list(size = 16, color ="White"), textangle = -90)), # list(text = "Casos acumulados (lineal)", # x = 1.05, # y = 0.7, # yref = "paper", # xref = "paper", # showarrow = FALSE, # font = list(size = 16, # color ="#ffd29f"), # textangle = -90)), hovermode = "closest", hoverdistance = 10, dragmode="pan", margin = list(l=50, r=50, b=75, t=60) ) %>% config(locale = "es", displaylogo=F, modeBarButtonsToRemove = c("select2d","zoom2d","lasso2d", "drawclosedpath","drawopenpath", "hoverClosestCartesian","hoverCompareCartesian", "toggleHover","toggleSpikelines") )%>% partial_bundle() ``` ### Acumulados - Logaritmico ```{r, message=F, warning=F} plots <- lapply(vars.pos, function(var) { plot_ly(dep.pos) %>% add_lines(x = ~dat, y = as.formula(paste0("~", var)), text = paste(dep.pos$days.end, "días desde hoy"), hovertemplate = paste('Fecha: %{x}', '
Media Móvil: %{y:.2f}
', '%{text}'), name = ifelse(var == last.pos,"Media Móvil",var), legendgroup = 'group1', showlegend = ifelse(var == last.pos,TRUE,FALSE), line = list(color = "#ffa600", width = 4) ) %>% add_segments(x = "2020-04-08", xend = "2020-04-08", y = 0, yend = max(dep.pos[var],na.rm = T), text="2020-04-08", name="Inicio de P.R's", hovertemplate = paste('%{text}'), legendgroup = 'group2', showlegend = ifelse(var == last.pos,TRUE,FALSE), line = list(color = "#7aa82a", width = 3, dash = "dot") ) %>% layout(xaxis = list(range = c(min(dep.pos$dat), max(dep.pos$dat)), color = "white"), yaxis = list(color = "white", type = "log", tickmode = "linear"), annotations = list(text = ifelse(var=="LA_LIBERTAD","LA LIBERTAD", ifelse(var=="MADRE_DE_DIOS","MADRE DE DIOS", ifelse(var=="SAN_MARTIN","SAN MARTIN",paste0("",var,"")))), x = 0,y = 1.15, yref = "paper",xref = "paper", xanchor = "left",yanchor = "top", showarrow = FALSE, font = list(size = 16, color = "white")), paper_bgcolor="black", plot_bgcolor="black", legend = list(orientation = "h", xanchor = "center", yanchor = "bottom", x = 0.5, y = -0.125, font = list(color = "white")), showlegend =T) }) subplot(plots,nrows=5, shareX = T, titleX = F) %>% layout(title = list(text = "Total de casos confirmados - Acumulado Logarítmico", font = list(size = 24, color="white")), annotations = list( list(text = "Fecha de reporte", x = 0.5, y = -0.09, yref = "paper", xref = "paper", showarrow = FALSE, font = list(size = 16, color = "white")), list(text = "Total de casos confirmados", x = -0.05, y = 0.5, yref = "paper", xref = "paper", showarrow = FALSE, font = list(size = 16, color ="white"), textangle = -90)), hovermode = "closest", hoverdistance = 10, dragmode="pan", margin = list(l=75, r=0, b=65, t=60) ) %>% plotly_config ``` Casos Nuevos {.bg} ===================================== Mapas {.tabset data-width=250} ------------------------------------- ### Casos nuevos ```{r, message=F, warning=F} x <- c.dep %>% dplyr::mutate(pos.new = ifelse(pos.new==0,NA, pos.new)) %>% .$pos.new labels.new <- sprintf( "%s
Casos: %s", c.dep$dep, c.dep$pos.new) %>% lapply(htmltools::HTML) pal.cases <- colorNumeric( palette="RdPu", domain = log(x), na.color="transparent") leaflet(c.dep) %>% map_tiles() %>% map_poly (y=x, z=labels.new) %>% addLegend("bottomleft", pal=pal.cases, values = log(x), title= 'Casos', labFormat = labelFormat(transform = function(x) round(exp(x))))%>% map_bounds() rm(x) ``` Column 2 {.tabset data-width=300} ------------------------------------- ### Diario y media móvil - Lineal ```{r, message=F, warning=F} plot_ly(nac) %>% add_trace(x = ~dat, y = ~pos.new, type = 'scatter', mode = 'lines', name = 'Casos Nuevos por día', line = list(color = '#006b7d'), text = paste(nac$days.end, "días desde hoy"), hovertemplate = paste('Fecha: %{x}', '
Nuevos Casos: %{y}
', '%{text}'))%>% add_trace(x = ~dat, y = ~mav.pos.new, type = 'scatter', mode = 'lines+markers', name = 'Media Móvil de casos por día', line = list(color = '#ffa600'), marker = list(color = '#ffa600'), text = paste(nac$days.end, "días desde hoy"), hovertemplate = ~paste('Fecha: %{x}', "
Media móvil: %{y:.0f}
", '%{text}')) %>% add_segments(x = "2020-04-08", xend = "2020-04-08", y = 0, yend=max(nac$pos.new), text="2020-04-08",name="Inicio de P.R's", hovertemplate = paste('%{text}'), legendgroup = 'group2', width=2, line = list(color = "#7aa82a", width = 3, dash = "dot") )%>% layout(title = ' Casos nuevos por día y media móvil', titlefont=list(color="white"), xaxis = list(title = "Fecha de reporte", color="white"), yaxis = list(side = 'left', title = 'Casos nuevos por día', showgrid = FALSE, zeroline = FALSE, color = "white"), yaxis2 = list(side = 'right', overlaying = "y", title = 'Casos nuevos (lineal)', showgrid = FALSE, zeroline = FALSE, color="#ffa600", range = c(min(0), max(nac$pos.new))), legend = list(orientation = "h", xanchor = "center", yanchor = "bottom", x = 0.5, y = -0.15, font = list(color = "white")), paper_bgcolor="black", plot_bgcolor="black", hovermode = "closest", hoverdistance = 50, dragmode="pan", margin = list(l=65, r=0, b=40, t=50) ) %>% config(locale = "es", displaylogo=F, modeBarButtonsToRemove = c("select2d","zoom2d","lasso2d", "drawclosedpath","drawopenpath", "hoverClosestCartesian","hoverCompareCartesian", "toggleHover","toggleSpikelines") ) ``` ### Media Móvil - logarítmica ```{r, message=F, warning=F} plot_ly(nac) %>% add_trace(x = ~dat, y = ~mav.pos.new, type = 'scatter', mode="lines",name = 'Casos Nuevos', line = list(color = '#006b7d'), text = paste(nac$days.end, "días desde hoy"), hovertemplate = paste('Fecha: %{x}', '
Nuevos Casos: %{y}
', '%{text}'))%>% add_trace(x = ~dat, y = ~mav.pos.new, type = 'scatter', mode = 'lines+markers', name = 'Media Móvil', line = list(color = '#ffa600'), marker = list(color = '#ffa600'), text = paste(nac$days.end, "días desde hoy"), hovertemplate = ~paste('Fecha: %{x}', "
Media móvil: %{y:.0f}
", '%{text}')) %>% add_segments(x = "2020-04-08", xend = "2020-04-08", y = 0, yend=max(nac$mav.pos.new), text="2020-04-08",name="Inicio de P.R's", hovertemplate = paste('%{text}'), legendgroup = 'group2', width=2, line = list(color = "#7aa82a", width = 3, dash = "dot") )%>% layout(title = 'Media móvil (7d) y casos nuevos por día - Perú', titlefont=list(color="white"), xaxis = list(title = "Fecha de reporte", color="white"), yaxis = list(side = 'left', title = 'Casos nuevos por día', showgrid = FALSE, zeroline = FALSE, color = "white", type ="log",tickmode="linear"), yaxis2 = list(side = 'right', overlaying = "y", title = 'Media móvil de casos nuevos - 7 días (lineal)', showgrid = FALSE, zeroline = FALSE, color="#ffa600", range = c(min(0), max(nac$mav.pos.new))), legend = list(orientation = "h", xanchor = "center", yanchor = "bottom", x = 0.5, y = -0.15, font = list(color = "white")), paper_bgcolor="black", plot_bgcolor="black", hovermode = "closest", hoverdistance = 50, dragmode="pan", margin = list(l=65, r=0, b=40, t=50) ) %>% config(locale = "es", displaylogo=F, modeBarButtonsToRemove = c("select2d","zoom2d","lasso2d", "drawclosedpath","drawopenpath", "hoverClosestCartesian","hoverCompareCartesian", "toggleHover","toggleSpikelines") ) ``` ### Duplicación de la media móvil ```{r, message=F, warning=F} plot_ly(dup.nac)%>% add_trace(x = ~dat, y = ~dup.1, mode = 'lines', name = 'Un (1) día', line = list(color = '#0e5871', dash = "dash", width=3.5), text = "Casos se duplican en un (1) día", hoverinfo = "text", legendgroup = 'group2') %>% add_trace(x = ~dat, y = ~dup.2, mode = 'lines', name = 'Dos (2) días', line = list(color = '#006b7d', dash = "dash", width=3.5), text = "Casos se duplican en dos (2) días", hoverinfo = "text", legendgroup = 'group2') %>% add_trace(x = ~dat, y = ~dup.3, mode = 'lines', name = 'Tres (3) días', line = list(color = '#007e7b', dash = "dash", width=3.5), text = "Casos se duplican en tres (3) días", hoverinfo = "text", legendgroup = 'group2') %>% add_trace(x = ~dat, y = ~dup.4, mode = 'lines', name = 'Cuatro (4) días', line = list(color = '#008f6a', dash = "dash", width=3.5), text = "Casos se duplican en cuatro (4) días", hoverinfo = "text", legendgroup = 'group2')%>% add_trace(x = ~dat, y = ~pos, type = 'scatter', mode = 'lines+markers', name = 'Casos Acumulados', line = list(color = '#ffa600'), marker = list(color = '#ffa600'), hovertemplate = ~paste("
Casos Acumulados: %{y:.d0}
"), legendgroup = 'group1') %>% layout(title = list(text= 'Tiempo de duplicación de casos acumulados', font = list( size = 20, color="white")), xaxis = list(title = list(text="Días desde el primer reporte", standoff = 15), range = c(as.Date(min(f.date)),max(today+15)), color ="white", tickformat= "%d-%b"), yaxis = list(side = 'left', title = list(text= 'Total de casos acumulados', font = list(size = 16, color = "white"), standoff = 15), type="log", automargin = T, range = c(min(0),max(6)), showgrid = FALSE, zeroline = FALSE, tickmode = "linear", tick0 = 0, color ="white"), legend = list(title=list(text="Casos se duplican en...", font = list(color="white"), side="top"), orientation = "h", yref = "paper", xref = "paper", xanchor = "right", yanchor = "bottom", x = 1, y = 0.1, font = list(color = "white"), automargin = T), annotations = list(xref='paper', yref='paper', x=0, y=-0.1, xanchor='left', yanchor='bottom', text='', showarrow=FALSE, font = list(size = 16, color = "white")), paper_bgcolor="black", plot_bgcolor="black", hovermode = "closest", hoverdistance = 50, dragmode="pan", margin = list(l=0, r=0, b=20, t=65) ) %>% plotly_config( ) ``` ```{r} dup.nac%>% dplyr::filter(days.end<=7 )%>% dplyr::mutate(days.start = as.numeric(difftime(dat,first(dat), units="days")), dummy = days.start+20, dup.1 = exp((log(2)/1)*days.start), dup.2 = exp((log(2)/2)*days.start), dup.3 = exp((log(2)/3)*days.start), dup.4 = exp((log(2)/4)*days.start), dup.6 = exp((log(2)/6)*days.start), dup.8 = exp((log(2)/8)*days.start), pos.sim = exp(((max(.$days.start)-min(.$days.start))/min(.$days.start))*(days.start))) %>% plot_ly()%>% add_trace(x = ~dat, y = ~pos.sim, type = 'scatter', mode = 'lines+markers', name = 'Casos Acumulados', line = list(color = '#ffa600'), marker = list(color = '#ffa600'), hovertemplate = ~paste("
Casos Acumulados: %{y:.d0}
"), legendgroup = 'group1') %>% add_trace(x = ~dat, y = ~dup.1, mode = 'lines', name = 'Casos se duplican en un (1) día', line = list(color = '#0e5871', dash = "dash", width=3.5), text = "Casos se duplican en un (1) día", hoverinfo = "text", legendgroup = 'group2') %>% add_trace(x = ~dat, y = ~dup.2, mode = 'lines', name = 'Casos se duplican en dos (2) días', line = list(color = '#006b7d', dash = "dash", width=3.5), text = "Casos se duplican en dos (2) días", hoverinfo = "text", legendgroup = 'group2') %>% add_trace(x = ~dat, y = ~dup.3, mode = 'lines', name = 'Casos se duplican en tres (3) días', line = list(color = '#007e7b', dash = "dash", width=3.5), text = "Casos se duplican en tres (3) días", hoverinfo = "text", legendgroup = 'group2') %>% add_trace(x = ~dat, y = ~dup.8, mode = 'lines', name = 'Casos se duplican en ocho (8) días', line = list(color = '#008f6a', dash = "dash", width=3.5), text = "Casos se duplican en ocho (8) días", hoverinfo = "text", legendgroup = 'group2') %>% layout(title = '', titlefont=list(color="white"), xaxis = list(title = "Últimos 7 días", range = c(as.Date(min(f.date)),max(today+15)), color ="white"), yaxis = list(side = 'left', title = '', type="log", range = c(min(0),max(3)), showgrid = FALSE, zeroline = FALSE, tickmode = "linear", tick0 = 0, color ="white"), legend = list(orientation = "h", xanchor = "center", yanchor = "bottom", x = 0.5, y = -0.25, font = list(color = "white")), paper_bgcolor="black", plot_bgcolor="black", hovermode = "closest", hoverdistance = 50, dragmode="pan", margin = list(l=50, r=50, b=30, t=50) ) %>% config(locale = "es", displaylogo=F, modeBarButtonsToRemove = c("select2d","zoom2d","lasso2d", "drawclosedpath","drawopenpath", "hoverClosestCartesian","hoverCompareCartesian", "toggleHover","toggleSpikelines") ) ``` Column 3 {.tabset data-width=350} ------------------------------------- ```{r, message=F, warning=F} y<- dep.pos.imp.new colnames(y) <- paste(colnames(y), "2", sep = "_") y<- y %>% select(-c("dat_2","days.end_2")) %>% cbind(dep.mav.pos.new) ``` ### Lineal ```{r, message=F, warning=F} plots <- lapply(vars.mav.new, function(var) { plot_ly(y) %>% add_lines(x = ~dat, y = as.formula(paste0("~", var)), text = paste(y$days.end, "días desde hoy"), hovertemplate = paste('Fecha: %{x}', '
Media Móvil: %{y:.2f}
', '%{text}'), name = ifelse(var == last.mav.new,"Media Móvil",var), legendgroup = 'group1', showlegend = ifelse(var == last.mav.new,TRUE,FALSE), line = list(color = "#ffa600", width = 4) ) %>% add_segments(x = "2020-04-08", xend = "2020-04-08", y = 0, yend = max(y[paste0(var,"_2")],na.rm = T), text="2020-04-08", name="Inicio de P.R's", hovertemplate = paste('%{text}'), legendgroup = 'group2', showlegend = ifelse(var == last.mav.new,TRUE,FALSE), line = list(color = "#7aa82a", width = 3, dash = "dot") )%>% add_trace(x = ~dat, y = as.formula(paste0("~", var,"_2")), type = 'bar', name = 'Casos Nuevos', marker = list(color = '#006b7d'), text = paste(y$days.end, "días desde hoy"), hovertemplate = paste('Fecha: %{x}', '
Nuevos Casos: %{y}
', '%{text}'), showlegend = ifelse(var == last.mav.new,TRUE,FALSE)) %>% layout(xaxis = list(range = c(min(y$dat), max(y$dat)), color = "white"), yaxis = list(color = "white"), annotations = list(text = ifelse(var=="LA_LIBERTAD","LA LIBERTAD", ifelse(var=="MADRE_DE_DIOS","MADRE DE DIOS", ifelse(var=="SAN_MARTIN","SAN MARTIN",paste0("",var,"")))), x = 0,y = 1.15, yref = "paper",xref = "paper", xanchor = "left",yanchor = "top", showarrow = FALSE, font = list(size = 16, color = "white")), paper_bgcolor="black", plot_bgcolor="black", legend = list(orientation = "h", xanchor = "center", yanchor = "bottom", x = 0.5, y = -0.125, font = list(color = "white")), showlegend =T) }) subplot(plots,nrows=5, shareX = T, titleX = F) %>% layout(title = list(text = "Media móvil (7 días) de casos nuevos", font = list(size = 24, color="white")), annotations = list( list(text = "Fecha de reporte", x = 0.5, y = -0.09, yref = "paper", xref = "paper", showarrow = FALSE, font = list(size = 16, color = "white")), list(text = "Media móvil - Número de casos", x = -0.05, y = 0.5, yref = "paper", xref = "paper", showarrow = FALSE, font = list(size = 16, color ="white"), textangle = -90)), hovermode = "closest", hoverdistance = 10, dragmode="pan", margin = list(l=75, r=0, b=65, t=60) ) %>% plotly_config() ``` ### Logaritmica ```{r, message=F, warning=F} plots <- lapply(vars.mav.new, function(var) { plot_ly(y) %>% add_lines(x = ~dat, y = as.formula(paste0("~", var)), text = paste(y$days.end, "días desde hoy"), hovertemplate = paste('Fecha: %{x}', '
Media Móvil: %{y:.2f}
', '%{text}'), name = ifelse(var == last.mav.new,"Media Móvil",var), legendgroup = 'group1', showlegend = ifelse(var == last.mav.new,TRUE,FALSE), line = list(color = "#ffa600", width = 4) ) %>% add_segments(x = "2020-04-08", xend = "2020-04-08", y = 0, yend = max(y[paste0(var,"_2")],na.rm = T), text="2020-04-08", name="Inicio de P.R's", hovertemplate = paste('%{text}'), legendgroup = 'group2', showlegend = ifelse(var == last.mav.new,TRUE,FALSE), line = list(color = "#7aa82a", width = 3, dash = "dot") )%>% add_trace(x = ~dat, y = as.formula(paste0("~", var,"_2")), type = 'bar', name = 'Casos Nuevos', marker = list(color = '#006b7d'), text = paste(y$days.end, "días desde hoy"), hovertemplate = paste('Fecha: %{x}', '
Nuevos Casos: %{y}
', '%{text}'), showlegend = ifelse(var == last.mav.new,TRUE,FALSE)) %>% layout(xaxis = list(range = c(min(y$dat), max(y$dat)), color = "white"), yaxis = list(color = "white", type="log"), annotations = list(text = ifelse(var=="LA_LIBERTAD","LA LIBERTAD", ifelse(var=="MADRE_DE_DIOS","MADRE DE DIOS", ifelse(var=="SAN_MARTIN","SAN MARTIN",paste0("",var,"")))), x = 0,y = 1.15, yref = "paper",xref = "paper", xanchor = "left",yanchor = "top", showarrow = FALSE, font = list(size = 16, color = "white")), paper_bgcolor="black", plot_bgcolor="black", legend = list(orientation = "h", xanchor = "center", yanchor = "bottom", x = 0.5, y = -0.125, font = list(color = "white")), showlegend =T) }) subplot(plots,nrows=5, shareX = T, titleX = F) %>% layout(title = list(text = "Media móvil (7 días) de casos nuevos", font = list(size = 24, color="white")), annotations = list( list(text = "Fecha de reporte", x = 0.5, y = -0.09, yref = "paper", xref = "paper", showarrow = FALSE, font = list(size = 16, color = "white")), list(text = "Media móvil - Número de casos", x = -0.05, y = 0.5, yref = "paper", xref = "paper", showarrow = FALSE, font = list(size = 16, color ="white"), textangle = -90)), hovermode = "closest", hoverdistance = 10, dragmode="pan", margin = list(l=75, r=0, b=65, t=60) ) %>% config(locale = "es", displaylogo=F, modeBarButtonsToRemove = c("select2d","zoom2d","lasso2d", "drawclosedpath","drawopenpath", "hoverClosestCartesian","hoverCompareCartesian", "toggleHover","toggleSpikelines") ) ``` Fallecidos {.bg} ===================================== Column 1 {.tabset data-width=250} ------------------------------------- Column 1 {.tabset data-width=300} ------------------------------------- Column 1 {.tabset data-width=350} ------------------------------------- Fallecidos Nuevos {.bg} ===================================== Diagnósticos {.bg} ===================================== Diagnósticos Nuevos {.bg} =====================================